package thread;

import java.util.concurrent.atomic.AtomicInteger;

public class TestDemo19 {
    public static void main(String[] args) throws InterruptedException {
        // 原子的
        AtomicInteger number = new AtomicInteger(0);

        // 两个线程同时操作 number
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                number.getAndIncrement();// +1
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                number.getAndIncrement();// +1
            }
        });
        t1.start();
        t2.start();

        t1.join();
        t2.join();
        System.out.println(number);
    }
}
